package config

import (
	"fmt"
	"os"
	"gitlab.gaodun.com/golib/gderror"
	"permission/vigo_loger"
)

type MySQLDriver struct {
	DriverName string
	DriverDns  string
}

const REDIS_PREFIX = "vigo_"

var VigoDebug = GetOsEnv()

var GdConfig, err = InitConfig()

func InitConfig() (map[string]string, error) {
	//config, err := consul.GetConf("permission")
	//if err != nil {
	//	return nil, err
	//}
	config:=make(map[string]string)
	config["VIGO_MYSQL_USER"]="permission"//"online-test"
	config["VIGO_MYSQL_PASSWORD"]="bTQ06RRHE1M5Dg"
	config["VIGO_MYSQL_DB_HOST"]="rm-uf6h71bx586l5o3i2rw.mysql.rds.aliyuncs.com"
	config["RDS_MYSQL_DB_PORT"]="3306"
	config["VIGO_MYSQL_DATABASE"]="permission"
	config["VIGO_REDIS_HOST"]="r-uf61e31b0e71d614.redis.rds.aliyuncs.com"
	config["VIGO_REDIS_PORT"]="6379"
	config["VIGO_REDIS_AUTH"]=""
	return config, nil
}

func GetMySQLConfig() (MySQLDriver, error) {
	config := GdConfig
	if err != nil {
		vigo_loger.AddGrayLog(gderror.New(err.Error()).Error())
		return MySQLDriver{}, err
	}
	// 数据库配置
	MySQLConfig := MySQLDriver{DriverName: "mysql", DriverDns: fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8", config["VIGO_MYSQL_USER"], config["VIGO_MYSQL_PASSWORD"], config["VIGO_MYSQL_DB_HOST"], config["RDS_MYSQL_DB_PORT"], config["VIGO_MYSQL_DATABASE"])}
	return MySQLConfig, nil
}

func GetLogConfig() (MySQLDriver, error) {
	config := GdConfig
	if err != nil {
		vigo_loger.AddGrayLog(gderror.New(err.Error()).Error())
		return MySQLDriver{}, err
	}
	// 数据库配置
	mySQLConfig := MySQLDriver{DriverName: "mysql", DriverDns: fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8", config["VIGO_LOG_MYSQL_USER"], config["VIGO_LOG_MYSQL_PASSWORD"], config["VIGO_LOG_MYSQL_HOST"], config["RDS_MYSQL_DB_PORT"], config["VIGO_LOG_MYSQL_DB"])}
	return mySQLConfig, nil
}

func GetOsEnv() bool {
	env := os.Getenv("SYSTEM_ENV")
	if env == "dev" {
		return true
	}
	return false
}

func GetIpWhiterList() string {
	return GdConfig["PUBLIC_ALIYUN_NGINX_NETWORK"] + ":" + GdConfig["PUBLIC_ALIYUN_APIGATEWAY_NETWORK"] + ":" + GdConfig["PUBLIC_GAODUN_OFFICE_NETWORK"] + ":" + GdConfig["PUBLIC_ALIYUN_ECS_NETWORK"]
}
